package com.example.easybuy.controller;

import com.example.easybuy.annotaion.CheckPermission;
import com.example.easybuy.entity.Address;
import com.example.easybuy.entity.CarDetail;
import com.example.easybuy.entity.OrderDetail;
import com.example.easybuy.service.OrderService;
import com.example.easybuy.util.AjaxResult;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("order")
public class OrderController {

    @Autowired
    private OrderService orderService;

    @RequestMapping("/getOrder")
    @CheckPermission({"1", "0"})
    public AjaxResult getOrder(@RequestParam Map<String,Object> map) {
        return AjaxResult.success().add("order", orderService.getOrder(map));
    }

    @PostMapping("/addOrder")
    @CheckPermission({"1", "0"})
    public AjaxResult addOrder(@RequestBody Map<String, String> map) {
        String json = map.get("selectProduct");
        String addressJson = map.get("address");
        String userId = map.get("userId");
        String loginName = map.get("loginName");
        ObjectMapper objectMapper = new ObjectMapper();
        List<CarDetail> list = new ArrayList<>();
        Address address=new Address();
        try {
            list = objectMapper.readValue(json, new TypeReference<ArrayList<CarDetail>>() {
            });
            address=objectMapper.readValue(addressJson,Address.class);
        } catch (JsonProcessingException e) {
            throw new RuntimeException(e);
        }
        return orderService.addOrder(list, loginName, Integer.parseInt(userId),address);
    }

    @RequestMapping("/getList")
    @CheckPermission({"1", "0"})
    public AjaxResult getList(@RequestParam Map<String, Object> map) {
        if (map.get("type").toString().equals("1")) {
            map.remove("userId");
        }
        if (map.get("serialNumber").toString().isEmpty()) {
            map.remove("serialNumber");
        }
        if (!map.get("selectUserId").toString().isEmpty()) {
            map.put("userId", map.get("selectUserId").toString());
        }
        return AjaxResult.success().add("pageInfo", orderService.orderByPage(map));
    }

    @PostMapping("/selectOrderDetail")
    @CheckPermission({"1", "0"})
    public AjaxResult selectOrderDetail(@RequestBody Map<String, Object> map) {
        List<OrderDetail> detailList = orderService.selectOrderDetail(map);
        return AjaxResult.success().add("detailList", detailList);
    }

    @RequestMapping("/updateOrder")
    @CheckPermission({"1", "0"})
    public AjaxResult updateOrder(@RequestParam Map<String, Object> map) throws Exception {
        return AjaxResult.success().add("isUpdate", orderService.updateOrder(map));
    }

    @RequestMapping("/delOrder")
    @CheckPermission({"1", "0"})
    public AjaxResult delOrder(@RequestParam Map<String, Object> map) {
        return AjaxResult.success().add("isDel", orderService.delOrder(map));
    }

}